//
//  ZGSMoreDIscountViewController.m
//  Tour
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSMoreDIscountViewController.h"
#import "AdvanceEdit.h"
#import "ZGSDestinationCity.h"
#import "ZGSDangDiYouCell.h"
#import "ScrollWebViewController.h"
#import "SaveTool.h"
#define HEADNAVIGATION self.navigationController.navigationBar.frame.size.height
@interface ZGSMoreDIscountViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@end

@implementation ZGSMoreDIscountViewController

- (void)dealloc{
    [_cityId release];
    [_name release];
    [_scrollView release];
    [_HUD release];
    [_tableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *strName = [NSString stringWithFormat:@"%@特价折扣Yeah!", self.name];
    self.title = strName;
    // Do any additional setup after loading the view.
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    [self.view addSubview:view];
//    [view release];
    [view release];
    UIButton *buttonFreedom = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonFreedom.frame = CGRectMake(0, 0, WIDTH / 2, 50);
    [buttonFreedom addTarget:self action:@selector(freedomAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonFreedom setTitle:@"超值自由行" forState:UIControlStateNormal];
    buttonFreedom.tintColor = [UIColor grayColor];
    [view addSubview:buttonFreedom];
    
    UIButton *buttonLocal = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonLocal.frame = CGRectMake(WIDTH / 2, 0, WIDTH / 2, 50);
    [buttonLocal addTarget:self action:@selector(localAction:) forControlEvents:UIControlEventTouchUpInside];
    [buttonLocal setTitle:@"精彩本地游" forState:UIControlStateNormal];
    buttonLocal.tintColor = [UIColor grayColor];
    [view addSubview:buttonLocal];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT - HEADNAVIGATION - 50) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[ZGSDangDiYouCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 150;
    [_tableView release];
    
    
    
    
    
    
//    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    self.HUD.labelText = @"正在加载";
//    self.HUD.dimBackground = YES;
    self.arr = [NSMutableArray array];
    [self creatData];
    
    
    
    
    
//    UIButton *button
}

- (void)creatData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&lat=38.88273654287845&lon=121.5394181426899&order=2&page=1&product_type=1016%%2C1018%%2C1020&time=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&type=1&v=1", self.cityId];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"][@"list"]];
        NSLog(@"%@", self.arr);
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDangDiYouCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:[self.arr[indexPath.row] photo]]placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
    NSString *str = [[self.arr[indexPath.row] price] substringWithRange:NSMakeRange(4, [self.arr[indexPath.row] price].length - 4)];
    NSMutableString *tmp = [NSMutableString stringWithFormat:@"%@", str];
    NSString *strTmp = [tmp stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.labelTitle.text = [self.arr[indexPath.row] title];
    cell.labelMoney.text = strTmp;
    cell.labelPrice.text = [self.arr[indexPath.row] priceoff];
    return cell;

}

- (void)freedomAction:(UIButton *)button{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&lat=38.88273654287845&lon=121.5394181426899&order=2&page=1&product_type=1016%%2C1018%%2C1020&time=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&type=1&v=1", self.cityId];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"][@"list"]];
        NSLog(@"%@", self.arr);
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];


    
}

- (void)localAction:(UIButton *)button{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@&lat=38.88273654287845&lon=121.5394181426899&order=2&page=1&product_type=2410&time=1&track_app_channel=App%%2520Store&track_app_version=6.7&track_device_info=iPhone7%%2C1&track_deviceid=CD8DD9AA-C227-45CD-A07E-DA485B537E39&track_os=ios%%25208.4.1&type=1&v=1", self.cityId];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.arr = [ZGSDestinationCity baseModelArr:dic[@"data"][@"list"]];
        NSLog(@"%@", self.arr);
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];

    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ScrollWebViewController *webVC = [[ScrollWebViewController alloc] init];
    NSString *str = [NSString stringWithFormat:@"http://z.qyer.com/deal/%@/", [self.arr[indexPath.row] countryid]];
    webVC.urlStr = str;
    [self presentViewController:webVC animated:YES completion:^{
        
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
